# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html
import xlwt


class FirstPipeline(object):

    wb = xlwt.Workbook()
    # cell_overwrite_ok=True表示允许覆盖单元格
    ws = wb.add_sheet("sheet1", cell_overwrite_ok=True)
    count = 0


    def process_item(self, item, spider):
        keys = []
        values = []
        for data in item:
            keys.append(data)
            values.append(item[data])

        if self.count == 0:
            for i in range(0, len(keys)):
                self.ws.write(0, i, keys[i])

        # 从第二行开始插入具体数据
        for i in range(0, len(values)):
            self.ws.write(1 + self.count, i, values[i])

        # 确定下一此要添加的行数
        self.count += 1
        return item


    def open_spider(self, spider):
        print("-" * 50)
        print("爬虫启动")
        print("-" * 50)


    def close_spider(self, spider):
        path = "虎牙5.xls"
        self.wb.save(path)
        print("已生成  {}".format(path))


